package com.sasken.pdb.models.columnmodels
{
	import com.sasken.pdb.controllers.remoting.DataModelRemoting;	
	import com.sasken.pdb.views.renderers.DataGridFilterHeaderRenderer;
	import com.sasken.pdb.views.renderers.LinkItemRenderer;
	
	import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
	import mx.core.ClassFactory;
	
	/**
	 *  This class is an example on how to use the column model. 
	 *  It will definitely not compile because a lot of stuff is missing due to 
	 *  NDA, but you can check the source code anyway.
	 * 
	 */
	public class ProjectColumnModel extends ColumnModel
	{
		
		/**
		 *  Constructor, initializes this object. All columns, their order and 
		 *  column header and item renderers are defined in the constructor statically. 
		 *  
		 * <p>If there is a need later to fetch this data from somewhere else, an association 
		 * to DataModelRemoting object can be created.</p>
		*/
		public function ProjectColumnModel()
		{
			super();
		
			headerColumnOrder = [0];
			contentColumnOrder = [0, 1, 2, 3, 4];
			
			
			_headerColumns = [new AdvancedDataGridColumn("name")];
			(_headerColumns[0] as AdvancedDataGridColumn).headerText = "Name";
			
			_contentColumns = [
					      new AdvancedDataGridColumn("number"),
					      new AdvancedDataGridColumn("projectStatus"),
					      new AdvancedDataGridColumn("startDateFormatted"),
					      new AdvancedDataGridColumn("endDateFormatted"),
					      new AdvancedDataGridColumn("site")			      
					      ];
			
			(_contentColumns[0] as AdvancedDataGridColumn).headerText = "Number";
			(_contentColumns[1] as AdvancedDataGridColumn).headerText = "Status";
			(_contentColumns[2] as AdvancedDataGridColumn).headerText = "Start date";
			(_contentColumns[3] as AdvancedDataGridColumn).headerText = "End Date";
			(_contentColumns[4] as AdvancedDataGridColumn).headerText = "Site";
			
			//////////////////////////////////
			// header renderers
			(_headerColumns[0] as AdvancedDataGridColumn).headerRenderer = new ClassFactory(DataGridFilterHeaderRenderer);			
						
			for(var i:int = 0; i < _contentColumns.length; ++i)
			{
				(_contentColumns[i] as AdvancedDataGridColumn).headerRenderer = new ClassFactory(DataGridFilterHeaderRenderer);	
			}
			
			//////////////////////////////////
			// item renderers			
			//(_contentColumns[9] as AdvancedDataGridColumn).itemRenderer = new ClassFactory(LinkItemRenderer);	
			
		}
		
		public override function getTitleForColumn(columnDataField:String):String
		{
			switch (columnDataField)
			{
				case "name":
					return (_headerColumns[0] as AdvancedDataGridColumn).headerText;
		
				case "number":
					return (_contentColumns[0] as AdvancedDataGridColumn).headerText;
				case "projectStatus":
					return (_contentColumns[1] as AdvancedDataGridColumn).headerText;
				case "startDateFormatted":
					return (_contentColumns[2] as AdvancedDataGridColumn).headerText;
				case "endDateFormatted":
					return (_contentColumns[3] as AdvancedDataGridColumn).headerText;
				case "site":
					return (_contentColumns[4] as AdvancedDataGridColumn).headerText;
	
			}
			return "error";
		}
		
		/**
		 * Returns data providers for column header filter combo fields.  
		 */ 
		public override function getDataProviderForColumnDataField(datafield:String):Object
		{
			switch(datafield)
			{				
				case "projectStatus":
					return DataRemoting.instance.projectStatusDataProvider;
					break;

				default:
					return null;
				
			}
			return null;
		}
		
		protected override function filterFunc(item:Object):Boolean
		{
			var pr:Project = Project(item);
				
			var matches:int = 0;
			if (_globalFilters.length)
			{
				for(var i:int = 0; i < _globalFilters.length; ++i)
				{
					var filter:String = String(_globalFilters[i]).toLowerCase();
					
					if  (pr.name &&
						(pr.name.toLowerCase().substring(0, filter.length) == filter) ||
						((pr.site &&
						 pr.site.toLowerCase().substring(0, filter.length) == filter)) ||
						((pr.projectStatus &&
						 pr.projectStatus.toLowerCase().substring(0, filter.length) == filter))      
						  
					   )
					{
						matches = 1;
						break;
					}
				}
				if (!matches)
				{
					return false;
				}
			}					
				
			if ((!filterStrings["name"] || 
				 pr.name.toLowerCase().substring(0, String(filterStrings["name"]).length) == String(filterStrings["name"]).toLowerCase()) &&

				(!filterStrings["projectStatus"] || 
				 pr.projectStatus.toLowerCase().substring(0, String(filterStrings["projectStatus"]).length) == String(filterStrings["projectStatus"]).toLowerCase()) 

			   )
			{
				return true;
			}
			else
			{
				return false;
			}
			
			
			
			return true;
		}

		
	}
}